/**

 @Name：layuiAdmin 用户管理 管理员管理 角色管理
 @Author：star1029
 @Site：http://www.layui.com/admin/
 @License：LPPL
    
 */


layui.define(['table', 'form'], function(exports){
  var $ = layui.$
  ,admin = layui.admin
  ,view = layui.view
  ,table = layui.table
  ,form = layui.form;
  //问题列表
  table.render({

    elem: '#LAY-customer-back-manage'
    ,method: 'post'
    ,where: {product_name: $('#product_name').val()}
    ,url: layui.setter.apiUrl+'/admin/customer_service/find_customer_service_list'
    ,page: true
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'id', width: 80, title: 'ID', sort: true}
      ,{field: 'product_name', title: '项目名称'}
      ,{field: 'user_name', title: '服务对象'}
      ,{field: 'department_name', title: '所属部门/分厂'}
      ,{field: 'position', title: '岗职位'}
      ,{field: 'feedback_content', title: '反馈内容'}
      ,{field: 'feedback_date', title: '反馈日期'}
      ,{field: 'customer_user_name', title: '客服人员'}
      ,{title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-customer-admin'}
    ]]
    ,text: '对不起，加载出现异常！'
  });
  
  //监听工具条
  table.on('tool(LAY-customer-back-manage)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.confirm('确定删除此数据？', function(index){
            $.ajax({
                type:'post',
                dataType:'json',
                data:{ "id": data.id },
                url: layui.setter.apiUrl+"/admin/customer_service/del_customer_service",
                success:function(json){
                    if (json.code == 0) {
                        layer.msg(result.msg, {icon:6, time:1500}, function() {
                            table.reload('LAY-customer-back-manage');
                        })
                    } else {
                        layer.msg(json.msg, { icon: 5, time:1500});
                    }
                }
            });
            obj.del();
            layer.close(index);
        });
    }else if(obj.event === 'edit'){
      admin.popup({
        title: '编辑客服台账'
        ,area: ['500px', '650px']
        ,id: 'LAY-popup-customer-add'
        ,success: function(layero, index){
          view(this.id).render('customer/customerform', data).done(function(){
            form.render(null, 'layuiadmin-form-admin');
            
            //监听提交
            form.on('submit(LAY-customer-back-submit)', function(data){
              var field = data.field; //获取提交的字段

              //提交 Ajax 成功后，关闭当前弹层并重载表格
              $.ajax({
                    type:'post',
                    dataType:'json',
                    data:field,
                    url: layui.setter.apiUrl+"/admin/customer_service/save_customer_service",
                    success:function(json){
                        if (json.code == 0) {
                            layer.msg(json.msg, {icon:6, time:1500});
                          layui.table.reload('LAY-customer-back-manage'); //重载表格
                          layer.close(index); //执行关闭 
                        } else {
                            layer.msg(json.msg, { icon: 5, time:1500});
                        }
                    }
                });
            });
          });
        }
      });
    }
  });

    exports('customer', {})
});